﻿#region Using statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace Rectangular.Model.Entities
{
    /* A delegate type for hooking up fire notifications */
    public delegate void FiredEventHandler(object sender, FiredEventArgs e);

    public class Player : Entity
    {
        #region Events
        public event FiredEventHandler Fired;
        #endregion

        #region Event triggers
        /// <summary>
        /// Triggers the fired event
        /// </summary>
        protected virtual void OnFired(FiredEventArgs e)
        {
            if (Fired != null)
            {
                Fired(this, e);
            }
        }
        #endregion

        #region Members
        /* The texture of the player */
        public static new Texture2D Texture;
        /* Time between shots */
        private float _rateOfFire = 250;
        /* Cooldown between shots */
        private float _coolDown = 0f;
        #endregion

        #region Constructors
        /// <summary>
        /// Constructs a new player
        /// </summary>
        public Player()
        {
            Width = 32;
            Height = 64;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Returns true if the fire cooldown is zero
        /// </summary>
        public bool CanFire
        {
            get { return _coolDown == 0; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Allows the player to fire
        /// </summary>
        public void Fire(Vector2 destination)
        {
            if (CanFire)
            {
                // Trigger fired event
                OnFired(new FiredEventArgs()
                {
                    Destination = destination
                });

                // Reset cooldown
                _coolDown = _rateOfFire;
            }
        }

        /// <summary>
        /// Updates the player status
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            // Controls rate of fire
            _coolDown = MathHelper.Clamp(_coolDown - (float)gameTime.ElapsedGameTime.Milliseconds, 0, _rateOfFire);
        }

        /// <summary>
        /// Draws the player
        /// </summary>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (Texture != null && Visible == true)
            {   
                // TODO: add rotation here
                spriteBatch.Draw(Texture, Position, Color.White);
            }
        }
        #endregion
    }
}